<?php

namespace app\v1\controller;

use support\Request;
use app\model\Util;
use app\model\BannerModel;
use app\model\GoodsCatModel;
use app\model\GoodsModel;
use app\model\PickUpPointModel;

class StoreController {
    
    public function index(Request $request){
        
    }

    public function config(Request $request){
        $data = BannerModel::whereNull('deleted_at')->where('type','=','store')->orderBy('sort_by','desc')->get();
        foreach($data as $item){
            $item->image = Util::pathToUrl($item->image);
        }

        $cats = GoodsCatModel::where('status','=','1')->where('fid','=','0')->orderBy('sort','desc')->get();
        foreach($cats as &$item){
            $item['image_url'] = Util::pathToUrl($item->image);
        }

        return json([
            'code' => 1,
            'data' => [
                'banners' => $data,
                'cats' => $cats,
            ],
        ]);
    }

    public function recommendGoodsList(Request $request){
        $type = $request->input('type');
        $query = GoodsModel::where('status','=','online');
        if($type == 'recommend'){
            $query->orderBy('sort_by','desc');
        }else{
            $query->orderBy('created_at','desc');
        }

        $goodsList = $query->limit(10)->get();
        foreach($goodsList as $item){
            $item['image_url'] = Util::pathToUrl($item->image);
            $item['min_price'] = $item->minPrice();

            $item['pre_sale'] = $item->pre_sale;
        }

        return json([
            'code' => 1,
            'data' => [
                'goodsList' => $goodsList,
            ],
        ]);
    }

    public function pickUpPointList(Request $request){
        $data = PickUpPointModel::where('status','=','1')->get();
        return json([
            'code' => 1,
            'data' => $data,
        ]);
    }

} 